package com.hzcx.app.simplechat.ui.chat;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.publicui.PublicRoundDialog;
import com.hzcx.app.simplechat.ui.setting.AddAutoSendMsgImageActivity;
import com.hzcx.app.simplechat.ui.setting.AddAutoSendMsgTextActivity;
import com.hzcx.app.simplechat.ui.setting.SettingAutoSendMsgActivity;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GroupSettingSearchActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;

    @BindView(R.id.group_search_setting_switch)
    Switch groupSearchSettingSwitch;
    @BindView(R.id.group_search_title)
    RelativeLayout groupSearchTitle;
    @BindView(R.id.group_search_type)
    TextView groupSearchType;

//    public static String GroupInfoBeanKey = "GroupSettingSearchActivityGroupInfoBean";
//    GroupInfoBean groupInfoBean;
    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;


    private int tag;
    private String originStr = "";

    @Override
    public int getLayout() {
        return R.layout.activity_group_setting_search;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("查找方式");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(GroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
        reStatus();
    }

    private void reStatus() {
        // 群属性(单选):Y=通过ID或群名查找,N=不可被查找,F=仅通过ID查找
        if (groupInfoBean.getGroup_property().equals("N")) {
            groupSearchTitle.setVisibility(View.GONE);
            groupSearchSettingSwitch.setChecked(false);
        } else {
            groupSearchTitle.setVisibility(View.VISIBLE);
            groupSearchSettingSwitch.setChecked(true);
            if (groupInfoBean.getGroup_property().equals("Y")) {
                groupSearchType.setText(R.string.group_search_id_key);
            } else {
                groupSearchType.setText(R.string.group_search_id);
            }
        }
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.group_search_setting_switch, R.id.group_search_type})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.group_search_setting_switch:
                if (groupSearchSettingSwitch.isChecked()) {
                    modifySearchGroup();
                } else {
                    modifySearchGroup();
                }
                break;
            case R.id.group_search_type:
                showAddDialog();
                break;
        }
    }


    private void modifySearchGroup() {
        GroupModel.modifyFindswitchGroup(this, groupInfoBean.getGroup_id(), new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                if (groupSearchSettingSwitch.isChecked()) {
                    groupInfoBean.setGroup_property("Y");
                    groupSearchTitle.setVisibility(View.VISIBLE);
                } else {
                    groupInfoBean.setGroup_property("N");
                    groupSearchTitle.setVisibility(View.GONE);
                }
            }
            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
            }
        });
    }

    /**
     * 添加自动回复弹窗
     */
    private void showAddDialog() {
        List<String> list = new ArrayList<>();

        list.add(getString(R.string.group_search_id_key));
        list.add(getString(R.string.group_search_id));
        PublicRoundDialog roundDialog = new PublicRoundDialog(this, "取消", list, (pos, menu) -> {
            switch (pos) {
                case 0:
                    modifyPropertyGroup(pos);
                    break;
                case 1:
                    modifyPropertyGroup(pos);
                    break;
            }
        });
        roundDialog.show();
    }

    private void modifyPropertyGroup(String originStr) {
        GroupModel.modifyPropertyGroup (this, groupInfoBean.getGroup_id(), originStr, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {

                ToastUtils.show("操作成功");
                //        群属性(单选):Y=通过ID或群名查找,N=不可被查找,F=仅通过ID查找
                groupInfoBean.setGroup_property(originStr);
                reStatus();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                ToastUtils.show("操作失败请重试");
            }
        });
    }


    private void modifyPropertyGroup(int tag) {
       String originStr = tag == 0 ? "Y" : "F";
       modifyPropertyGroup(originStr);
    }

}